const app = getApp();
const common = require('../../../utils/common.js');
const api = require('../../../config/api.js');

var isOperate = 0;

Page({
  data: {
    selected: 0,
    chargeList: [],
    amount: '',
    hidden: true
  },
  onLoad: function (options) {
		this.setData({
			phoneNumber: app.globalData.phoneNumberStr
		});
		this.getMemberInfo();
    this.getChargeList();
  },
	getMemberInfo: function () {
		var that = this;
		var data = {};
		common.request(api.MemberInfo, data, 'get').then(function (data) {
			if (data.type == 'success') {
				that.setData({
					nickname: data.data.nickname
				});
			}
		});
	},
  getChargeList: function () {
    var that = this;
    common.request(api.ReChargeList, {}, 'POST').then(function (data) {
      if (data.type == 'success') {
        that.setData({
          chargeList: data.data.chargeList
        })
      } else {
        console.log(data.content);
      }    
    });
  },
  radioChange: function (e) {
    this.setData({
      selected: e.target.dataset.radiovalue,
      amount: '',
      hidden: false
    })
  },
  inputFocus:function(e){
    this.setData({
      selected: 0,
    })
  },
  bindAmountChange: function (e) {
    var amount = e.detail.value
    this.setData({
      amount: amount
    })
    var selected = this.data.selected;
    if (selected != 0) {
      this.setData({
        hidden: false,
        amount: ''
      })
    } else {
      if (amount != null && amount != '') {
        this.setData({
          hidden: false
        })
      } else {
        this.setData({
          hidden: true
        })
      }
    }
  },
  toRecharge: function () {
    if (isOperate == 1) {
      return false;
    }
    isOperate = 1;
    var that = this;
    var rechargeTypeId = that.data.selected;
    var amount = that.data.amount;
    if (rechargeTypeId == 0 && (amount == null || amount == '')) {
      common.showErrorToast('请选择或输入充值金额');
      isOperate = 0;
      return false;
    }
    var data = {
      rechargeTypeId: rechargeTypeId,
      amount: amount
    }
    common.request(api.RechargeInfo, data, 'POST').then(function (data) {
      if (data.type == 'success') {
        wx.requestPayment({
          timeStamp: data.data.timeStamp,
          nonceStr: data.data.nonceStr,
          package: data.data.package,
          signType: data.data.signType,
          paySign: data.data.paySign,
          success: function (res) {
						common.showSuccessToast('充值成功');
            setTimeout(function () {
              wx.navigateBack();
            }, 1500)
            isOperate = 0 ;
          },
          fail: function (res) {
						common.showErrorToast('充值失败');
            isOperate = 0;
          }
        })
      } else {
				common.showErrorToast(data.content);
        isOperate = 0;
      }
    })
  }
})